<template>
	<color-header height="900rpx" page-color="#fff">
		<image class="mine-bg" src="/static/images/mine-bg.png" mode="aspectFill"></image>
	</color-header>
	<uni-nav-bar title="账户登录" left-icon="left" clear-float status-bar background-color="transparent" :border="false"
	 @clickLeft="$navTo('',{}, 'navigateBack')"></uni-nav-bar>	
	
	<view class="mobile-login-wrapper">
		<image class="logo" src="/static/images/logo.png" mode="aspectFill"></image>
		<view class="logo-title">See Wow</view>
		<view class="form-wrapper">
			<view class="form-item">
				<input class="form-ipt" type="number" v-model="phone" placeholder="请输入手机号" maxlength="11" @input="validatePhone" />
			</view>
			<view class="form-item code-item">
				<input class="code-ipt" type="number" placeholder="请输入手机验证码" v-model="code" />
				<button class="btn code-btn" :class="{'btn-disabled': !phoneCorrect}" :disabled="!phoneCorrect" hover-class="tap-hover" @click="getCode">{{ codeText }}</button>
			</view>
		</view>
		<checkbox-group @change="checkChange">
			<view class="protocol-wrapper">
				<label class="label-check">
					<checkbox class="circle" style="transform: scale(0.8);" :value="1" :checked="isChecked"
					activeBackgroundColor="#00CAC0" icon-color="#fff"  />
					<text>已阅读并同意</text>
				</label>
				<text class="theme-color" @click="toProto(1)">《See Wow用户协议》</text>、
				<text class="theme-color" @click="toProto(2)">《See Wow隐私政策》</text>
			</view>
		</checkbox-group>
		
		<button class="btn login-btn" :class="{'btn-disabled': !phoneCorrect || !code}" hover-class="tap-hover"
		:disabled="!phoneCorrect || !code" @click="mobileLogin">确定</button>
		
	</view>
	
	<modal ref="modal">
		<view class="protocol-modal">
			<text>已阅读并同意</text>
			<text class="theme-color" @click="toProto(1)">《See Wow用户协议》</text>、
			<text class="theme-color" @click="toProto(2)">《See Wow隐私政策》</text>
		</view>
	</modal>
</template>

<script>
	import { debounce } from '../../common/utils';
	import { useUserStore } from '../../store/userStore';
	export default {
		data() {
			return {
				userStore: useUserStore(),
				wx_code: '',  // 微信登录临时code
				phone: '',  // 手机号
				code: '', // 验证码
				codeText: '获取验证码',
				seconds: 0,
				timer: null,
				isChecked: false,
				phoneCorrect: false,
				validatePhoneDebounce: null
			}
		},
		onLoad() {
			this.validatePhoneDebounce = debounce((phone) => {
				let regExp = /^[1][3-9]\d{9}$/;
				if (regExp.test(phone)) {
					this.phoneCorrect = true
				} else {
					this.phoneCorrect = false;
				}
			}, 300, false)
			
			const eventChannel = this.getOpenerEventChannel()
			eventChannel.on('wxCode', (data) => {
				this.wx_code = data.wx_code
			})
		},
		methods: {
			toProto(type) {
				uni.navigateTo({
					url: '/pages/proto/index?type=' + type
				})
			},
			validatePhone(e) {
				this.validatePhoneDebounce(e.detail.value)
			},
			// 手机验证码登录
			async mobileLogin() {
				if (!this.isChecked) {
					this.$refs.modal.show({
						title: '温馨提示',
						confirmText: '同意',
						cancelText: '不同意',
						success: (res) => {
							if (res.confirm) {
								this.isChecked = true
							}
						}
					})
					return
				}
				if (!this.phone) {
					uni.showToast({
						title: '请输入手机号',
						icon: 'none'
					})
					return
				}
				if (!this.code) {
					uni.showToast({
						title: '请输入手机验证码',
						icon: 'none'
					})
					return
				}
				const res = await this.$request.post('/api/login/login', {
					phone: this.phone,
					type: 1,
					code: this.code,
					wx_code: this.wx_code
				})
				// console.log(res);
				if (res.code == 200) {
					uni.setStorageSync('userInfo', res.data)
					uni.setStorageSync('history-user', res.data)
					uni.setStorageSync('token', res.data.token)
					this.userStore.userInfo = res.data
					uni.$emit('loginSuccess')
					setTimeout(() => {
						uni.navigateBack({
							delta: 2
						})
					}, 300)
				}
			},
			// 获取验证码
			async getCode() {
				if (!this.phone) {
					uni.showToast({
						title: '请输入手机号',
						icon: 'none'
					})
					return
				}
				if (this.seconds > 0) {
					return
				}
				this.seconds = 60
				this.timer = setInterval(() => {
					this.seconds --
					this.codeText = `剩余${this.seconds}s`
					if (this.seconds == 0) {
						clearInterval(this.timer)
						this.codeText = '重新获取'
					}
				}, 1000)
				const res = await this.$request.post('/api/login/sms', {
					phone: this.phone
				})
				if (res.code == 200) {
					uni.showToast({
						title: '验证码已发送,请注意查收',
						icon: 'none'
					})
				}
			},
			checkChange(e) {
				const { value } = e.detail
				this.isChecked = value != false ? true : false
			},
		}
	}
</script>

<style>
	page {
		background-color: #F8FAFD;
	}
</style>
<style scoped lang="scss">
	.mine-bg {
		width: 100%;
		height: 900rpx;
	}
	.mobile-login-wrapper {
		border-radius: 32rpx 32rpx 0 0;
		position: relative;
		display: flex;
		flex-direction: column;
		align-items: center;
		padding: 24rpx 32rpx;
		margin-top: 100rpx;
		
		.logo{
			width: 160rpx;
			height: 160rpx;
			border-radius: 50%;
			background-color: #eee;
		}
		.logo-title {
			font-size: 34rpx;
			font-weight: 500;
			margin-top: 32rpx;
		}
		.mobile-confirm-btn {
			width: 100%;
			margin-top: 60rpx;
		}
		.mobile-title {
			font-weight: 500;
			text-align: center;
		}
		.form-wrapper {
			padding: 0 24rpx;
			background-color: #fff;
			border-radius: 24rpx;
			width: 100%;
			margin-top: 80rpx;
		}
		.form-item {
			box-sizing: border-box;
			width: 100%;
			height: 120rpx;
			display: flex;
			align-items: center;
			&:first-child {
				border-bottom: 1px solid rgba(0, 0, 0, 0.10);
			}
			.form-ipt {
				width: 100%;
			}
		}
		.code-item {
			display: flex;
			align-items: center;
			justify-content: space-between;
		}
		.code-btn {
			font-size: 28rpx;
			color: #fff;
			background-color: $uni-color-primary;
			height: 64rpx;
			line-height: 64rpx;
			border-radius: 32rpx;
			width: 220rpx;
			text-align: center;
			margin: 0;
		}
		.login-btn {
			width: 100%;
			height: 100rpx;
			border-radius: 50rpx;
			background-color: $uni-color-primary;
			color: #fff;
			line-height: 100rpx;
			margin-top: 80rpx;
		}
		.btn-disabled {
			background-color: #E8E8E8;
			color: #999;
		}
		.protocol-wrapper {
			margin-top: 20rpx;
			width: 100%;
			text-align: left;
			font-size: 24rpx;
			.theme-color {
				color: $uni-color-primary;
			}
			.label-check {
				display: inline-flex;
				align-items: center;
			}
			
		}
		
	}
	
	.protocol-modal {
		font-size: 26rpx;
		margin-top: 32rpx;
		padding: 0 32rpx;
		text-align: center;
		.theme-color {
			color: $uni-color-primary;
		}
	}
</style>